﻿if (typeof jQuery === "undefined") {
    throw new Error("ui requires jQuery");
}
if (typeof whoever === "undefined") {
    throw new Error("ui requires whoever");
}
if (typeof moment === "undefined") {
    throw new Error("ui requires moment");
}
if (typeof toastr === "undefined") {
    throw new Error("ui requires toastr");
}

var ui = (function ($, _, whoever, toastr) {
    "use strict";

    toastr.options = {
        "closeButton": false,
        "debug": false,
        "positionClass": "toast-top-full-width",
        "onclick": "",
        "timeOut": "0",
        "showDuration": "300",
        "hideDuration": "300",
        "extendedTimeOut": "0",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    };

    var _message = (function () {
        var messageWithAction = function (options) {
            var message = "";
            var actions = options.Actions ? options.Actions : [];

            for (var i in actions) {
                var action = actions[i];
                message += "<button class='" + action.CssClass + "' id='action_" + i + "'><span><b>" + action.Text + "</b></span></button>";
            }

            return message;
        };

        var messageWithActionBehavior = function (options) {
            var actions = options.Actions ? options.Actions : [];

            for (var i in actions) {
                var action = actions[i];

                $("#action_" + i).bind("click", { action: action }, function (event) {
                    event.data.action.Behavior();
                });
            }
        };

        return {
            Info: function (message) {
                toastr.info(message);
            },
            Success: function (message) {
                toastr.success(message);
            },
            Warning: function (message) {
                toastr.warning(message);
            },
            Error: function (message) {
                toastr.error(message);
            },
            Clear: function () {
                toastr.clear();
            },
            MessageWithAction: function (options) {
                switch (options.MessageType) {
                    case 0:
                        Main.CRUD.InfoWithAction(options);
                        break;
                    case 1:
                        Main.CRUD.SuccessWithAction(options);
                        break;
                    case 2:
                        Main.CRUD.WarningWithAction(options);
                        break;
                    case 3:
                        Main.CRUD.ErrorWithAction(options);
                        break;
                    default:
                        Main.CRUD.ErrorWithAction(options);
                        break;
                }
            },
            InfoWithAction: function (options) {
                ui.Message.Info((options.Message || "Información disponible,") + " " + messageWithAction(options));
                messageWithActionBehavior(options);
            },
            SuccessWithAction: function (options) {
                ui.Message.Success((options.Message || "La operación se realizó con éxito,") + " " + messageWithAction(options));
                messageWithActionBehavior(options);
            },
            WarningWithAction: function (options) {
                ui.Message.Warning((options.Message || "La operación se realizó con errores,") + " " + messageWithAction(options));
                messageWithActionBehavior(options);
            },
            ErrorWithAction: function (options) {
                ui.Message.Error((options.Message || "La operación no se realizó con éxito,") + " " + messageWithAction(options));
                messageWithActionBehavior(options);
            }
        };
    })();

    var _formating = (function () {
        return {
            Date: function (date) {
                return moment(date).format("MM D YYYY");
            },
            DateTime: function (date) {
                return moment(date).format('lll');
            }
        };
    })();

    var _animation = (function () {
        return {
            MainExit: function () {
                return whoever.Animation.MainExitRight();
            },
            MainEnter: function () {
                return whoever.Animation.MainEnter();
            },
            MainInit: function () {
                whoever.Animation.MainRight();
                whoever.Animation.MainVisible();
            },
            ShowLoading: function () {
                whoever.Form.DisableAll();
                ui.Message.Info("Loading...");
            },
            HideLoading: function (callback) {
                $.when(ui.Message.Clear()).done(function () {
                    if (callback)
                        setTimeout(callback, 1200);
                });
            }
        };
    })();

    var _form = (function () {
        return {
            FormEnabling: function (page, disabledPages) {
                if (_.contains(disabledPages, page)) {
                    whoever.Form.DisableAll();
                } else {
                    whoever.Form.EnableAll();
                }

                whoever.Form.EnableButtons();
            }
        };
    })();

    return {
        Formating: _formating,
        Animation: _animation,
        Form: _form,
        Message: _message
    };
})(jQuery, _, whoever, toastr);